package com.xuequ.answerme.question;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.xuequ.answerme.R;
import com.xuequ.answerme.beans.AnswerBean;
import com.xuequ.answerme.beans.QuestionBean;
import com.xuequ.answerme.beans.UserBean;
import com.xuequ.answerme.manager.AnswerManager;

import cn.bmob.v3.BmobUser;

/**
 * Created by Shijie on 2015/10/12.
 */
public class AnswerQuestionActivity extends AppCompatActivity{

    private EditText mAnswerEditText;
    private ImageView mAnswerImg;
    private QuestionBean questionBean;
    private Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_answer);

        initViews();
    }

    private void initViews() {

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        questionBean = (QuestionBean) bundle.getSerializable("questionBean");

        toolbar = (Toolbar) findViewById(R.id.answer_tool_bar);
        setSupportActionBar(toolbar);//ToolBar替代ActionBar
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        mAnswerEditText = (EditText) findViewById(R.id.answer_question_description_editText);
        mAnswerImg = (ImageView) findViewById(R.id.answer_question_answer_imageView);

        mAnswerImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(TextUtils.isEmpty(mAnswerEditText.getText())){
                    Log.i("AnswerQuestionActivity","there is no answer.");
                }else{
                    AnswerBean answerBean = new AnswerBean();
                    answerBean.setAnswerer(BmobUser.getCurrentUser(AnswerQuestionActivity.this, UserBean.class));
                    answerBean.setAnswer_content(mAnswerEditText.getText().toString());
                    answerBean.setAnswer_likes(0);
                    answerBean.setAnswer_of_question(questionBean);

                    new AnswerManager().answerQuestion(AnswerQuestionActivity.this, answerBean, new AnswerManager.AnswerQuestionListener() {
                        @Override
                        public void answerQuestionOnSuccess() {
                            finish();
                        }

                        @Override
                        public void answerQuestionOnFailure(String failureMsg) {

                        }
                    });

                }
            }
        });

    }

}
